package com.solotiger.geographic.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.solotiger.geographic.R;
import com.solotiger.geographic.common.GlobalConstants;
import com.solotiger.geographic.ui.base.BaseActivity;
import com.solotiger.geographic.util.FileUtil;
import com.solotiger.geographic.util.SystemUtil;

import java.io.File;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class SettingActivity extends BaseActivity {

    @BindView(R.id.tv_size)
    TextView mTvSize;

    @BindView(R.id.layout_clear)
    View mBtnClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init();
    }

    private void init() {
        setTitle("设置");
        getToolbar().setNavigationIcon(R.drawable.back);
        setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        long size = FileUtil.getFolderSize(new File(GlobalConstants.GLOBAL_CACHE_PATH));
        mTvSize.setText(FileUtil.getFormatSize(size));

        RxView.clicks(mBtnClear)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        FileUtil.deleteFolderFile(GlobalConstants.GLOBAL_CACHE_PATH, false);
                        mTvSize.setText("0KB");
                        showMessage("缓存清除成功!");
                    }
                });
    }

    @OnClick({R.id.layout_about, R.id.layout_collect})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_about:
                showMessage("当前最新版本为V1.0");
                break;
            case R.id.layout_collect:
                SystemUtil.startActivity(mContext, new Intent(mContext, CollectActivity.class));
                break;
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_setting;
    }
}
